﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <input type="button" value="获取本机所有LocalStorage" id="btn1" />
</body>
</html>
<script type="text/javascript">

    var LocalStroageControl = function () {
        this.isLocalStorage = window.localStorage ? true : false;

        this.set = function (key, value) {
            if (this.isLocalStorage) {
                window.localStorage.setItem(key, value);
            }
            else {
                return;
            }
        }

        this.get = function (key) {
            if (this.isLocalStorage) {
                return window.localStorage.getItem(key);
            }
            else {
                return null;
            }
        }

        this.remove = function (key) {
            if (this.isLocalStorage) {
                localStorage.removeItem(key);
            }
            else {
                return;
            }
        }

        this.getAllLocalStroage = function () {
            var arrKey = new Array();
            var arrVal = new Array();
            var json = "";
            for (var i in localStorage) {
                arrKey.push(i);
            }
            if (arrKey.length == 0) {
                alert("there are no localStorages in your computer!");
            }
            else {
                for (var i = 0; i < arrKey.length; i++) {
                    arrVal.push(this.get(arrKey[i]));
                }
                json = "[";
                for (var i = 0; i < arrKey.length; i++) {
                    json += "{";
                    json += arrKey[i] + ":" + "'" + arrVal[i] + "'";
                    json += "}";
                    json += ",";
                }
                json = json.substring(0, json.lastIndexOf(','));
                json += "]";
            }

            return json;
        }
    }

    var localData = new LocalStroageControl();
    
    localData.set("msg1", "abc");
    localData.set("msg2", "ss");

    document.getElementById("btn1").onclick = function () {
        alert(localData.getAllLocalStroage());
    }
</script>
